package com.powernode.map.controller;

import com.powernode.common.result.Result;
import com.powernode.map.service.LocationService;
import com.powernode.model.form.map.OrderServiceLocationForm;
import com.powernode.model.form.map.SearchNearByDriverForm;
import com.powernode.model.form.map.UpdateDriverLocationForm;
import com.powernode.model.form.map.UpdateOrderLocationForm;
import com.powernode.model.vo.map.NearByDriverVo;
import com.powernode.model.vo.map.OrderLocationVo;
import com.powernode.model.vo.map.OrderServiceLastLocationVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 位置控制器
 * 提供更新配送员位置信息到Redis地理空间索引和删除配送员位置信息的功能
 */
@Slf4j
@Tag(name = "位置API接口管理")
@RestController
@RequestMapping("/map/location")
@SuppressWarnings({"unchecked", "rawtypes"})
public class LocationController {
    @Resource
    private LocationService locationService;

    /**
     * 更新配送员位置信息到redis geo中
     *
     * @param updateDriverLocationForm 包含配送员位置信息的表单对象
     * @return 操作成功结果
     */
    @Operation(summary = "更新配送员位置信息到redis geo中")
    @PostMapping("/updateDriverLocation")
    public Result<Boolean> updateDriverLocation(@RequestBody UpdateDriverLocationForm updateDriverLocationForm) {
        return Result.ok(locationService.updateDriverLocation(updateDriverLocationForm));
    }

    /**
     * 删除配送员位置信息
     *
     * @param driverId 配送员ID
     * @return 操作成功结果
     */
    @Operation(summary = "删除配送员位置信息")
    @DeleteMapping("/removeDriverLocation/{driverId}")
    public Result<Boolean> removeDriverLocation(@PathVariable Long driverId) {
        return Result.ok(locationService.removeDriverLocation(driverId));
    }

    /**
     * 查询附近配送员
     *
     * @param searchNearByDriverForm 包含查询条件的表单对象
     * @return 附近配送员列表
     */
    @Operation(summary = "查询附近配送员")
    @PostMapping("/searchNearByDriver")
    public Result<List<NearByDriverVo>> searchNearByDriver(@RequestBody SearchNearByDriverForm searchNearByDriverForm) {
        return Result.ok(locationService.searchNearByDriver(searchNearByDriverForm));
    }

    /**
     * 更新订单位置到缓存
     *
     * @param updateOrderLocationForm 包含订单位置信息的表单对象
     * @return 操作成功结果
     */
    @Operation(summary = "更新配送员位置")
    @PostMapping("/updateOrderLocationToCache")
    Result<Boolean> updateOrderLocationToCache(@RequestBody UpdateOrderLocationForm updateOrderLocationForm){
        return Result.ok(locationService.updateOrderLocationToCache(updateOrderLocationForm));
    }

    /**
     * 从缓存中获取订单位置
     *
     * @param orderId 订单ID
     * @return 订单位置信息
     */
    @Operation(summary = "获取缓存中配送员的位置信息")
    @GetMapping("/getCacheOrderLocation/{orderId}")
    public Result<OrderLocationVo> getCacheOrderLocation(@PathVariable Long orderId) {
        return Result.ok(locationService.getCacheOrderLocation(orderId));
    }

    /**
     * 开始配送服务：保存配送服务订单位置
     *
     * @param orderLocationServiceFormList 包含配送服务订单位置信息的表单对象列表
     * @return 操作成功结果
     */
    @Operation(summary = "开始配送服务：保存配送服务订单位置")
    @PostMapping("/saveOrderServiceLocation")
    public Result<Boolean> saveOrderServiceLocation(@RequestBody List<OrderServiceLocationForm> orderLocationServiceFormList) {
        return Result.ok(locationService.saveOrderServiceLocation(orderLocationServiceFormList));
    }

    /**
     * 配送服务获取订单服务最后一个上传位置信息
     *
     * @param orderId 订单ID
     * @return 订单服务最后一个上传位置信息
     */
    @Operation(summary = "配送服务获取订单服务最后一个上传位置信息")
    @GetMapping("/getOrderServiceLastLocation/{orderId}")
    public Result<OrderServiceLastLocationVo> getOrderServiceLastLocation(@PathVariable Long orderId) {
        return Result.ok(locationService.getOrderServiceLastLocation(orderId));
    }

    /**
     * 计算订单实际里程
     *
     * @param orderId 订单ID
     * @return 订单实际里程
     */
    @Operation(summary = "计算订单实际里程")
    @GetMapping("/calculateOrderRealDistance/{orderId}")
    public Result<BigDecimal> calculateOrderRealDistance(@PathVariable Long orderId) {
        return Result.ok(locationService.calculateOrderRealDistance(orderId));
    }
}

